package edu.wuster.blog.controller;

/*
 *
 * 文件上传controller类
 * */

import edu.wuster.blog.entity.Result;
import edu.wuster.blog.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin
@Slf4j
@RestController
public class UploadController {

    private final AliOSSUtils aliOSSUtils;

    //构造器注入
    public UploadController(AliOSSUtils aliOSSUtils) {
        this.aliOSSUtils = aliOSSUtils;
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        //日志输出信息
        log.info("正在进行文件上传：{}", image.getOriginalFilename());

        //调用阿里云工具类进行文件上传
        String url = aliOSSUtils.upload(image);

        log.info("文件上传成功：{}", url);

        //返回结果
        return Result.success(url);
    }
}
